'use strict';

module.exports = () => async (ctx, next) => {
  ctx.designatedStaffId = '';
  ctx.designatedPlatId = '';
  ctx.role = '';
  ctx.jwterror = '';
  const token = ctx.get('Authorization');
  if (ctx.app.config.env !== 'prod' && !token) {
    ctx.designatedStaffId = ctx.get('yc-designated-staff-id');
    ctx.designatedPlatId = ctx.get('yc-designated-plat-id');
  }
  if (token) {
    try {
      const {
        designatedStaffId,
        designatedPlatId,
        isolation,
      } = await ctx.helper.verify(token);
      ctx.designatedStaffId = designatedStaffId;
      ctx.designatedPlatId = designatedPlatId;
      ctx.isolation = isolation || '';
    } catch (e) {
      ctx.throw(200, { code: -402, message: 'token失效' });
    }
  }
  await next();
};
